package wjb.test.mawen.mawen.controller;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import wjb.test.mawen.mawen.mapper.UserMapper;
import wjb.test.mawen.mawen.model.User;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class IndexController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/")
    public  String hello(HttpServletRequest httpServletRequest){
        //检查是否有本地的token
        User user=null;
        Cookie[] cookies = httpServletRequest.getCookies();
        if (cookies !=null){
            for (Cookie cookie:cookies){
                if("mawentoken".equals(cookie.getName())){
                    String token=cookie.getValue();
                    user= userMapper.findUserByToken(token);
                    break;
                }
            }
        }


        httpServletRequest.getSession().setAttribute("user",user);
        return "index";
    }
}
